/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.help.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.oss.OSSServer;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.help.entity.AppCan;
import com.google.common.collect.Maps;

/**
 * appCan实例Controller
 * 
 * @author lvbo
 * @version 2013-07-25
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/help/appCan")
public class AppCanController extends BaseController {

	Logger log = Logger.getLogger(UploadController.class);

	@ModelAttribute
	public AppCan get(@RequestParam(required = false) Long id) {
		return new AppCan();
	}

	@RequestMapping(value = "form")
	public String form(AppCan appCan, Model model) {
		return "modules/help/appCanForm";
	}

	@ResponseBody
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") MultipartFile file, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String fileUrl = "";
		try{
			if (!file.isEmpty()) {
				fileUrl = OSSServer.save(file);
			}	
			log.debug("上传成功："+fileUrl);
		} catch(Exception e) {
			log.debug("上传失败："+e);
		}
		Map<String, String> map = Maps.newHashMap();
		map.put("localName", file.getOriginalFilename());
		map.put("serverName", fileUrl);
		return JsonMapper.getInstance().toJson(map);
	}

}
